﻿<?php    
    $date = getdate();
    $strdate = $date['year'].'-'.$date['mon'].'-'.$date['mday'].'_'.
                $date['hours'].'-'.$date['minutes'].'-'.$date['seconds'];
    $fmtdate = $date['mday'].'/'.$date['mon'].'/'.$date['year'].' '.
                $date['hours'].':'.$date['minutes'].':'.$date['seconds'];
    $name=$cb['User']['canbo_id'].'_'.$strdate;//date('Y-m-d_h-m-s');

    $worksheets = '';
    
    
      
    
    // Sheet đầu tiên lưu thông tin của cán bộ
    $rows = $xls->row($xls->hcell('UID').$xls->hcell('CBID').$xls->hcell('Họ tên').$xls->hcell('Thời điểm tải về'));
    $rows .= $xls->row($xls->cell($cb['User']['id']).$xls->cell($cb['User']['canbo_id']).$xls->cell($cb['Canbo']['hoten_cb']).$xls->cell($fmtdate));
    $worksheets .= $xls->worksheet('ThongTin', $rows);
    
    
    
    
    $rows = $xls->row(
            $xls->hcell('id').$xls->hcell('tentuyen').
            $xls->hcell('tddau').$xls->hcell('hddau').
            $xls->hcell('tdgiua').$xls->hcell('hdogiua').
            $xls->hcell('tdcuoi').$xls->hcell('hdocuoi').
            $xls->hcell('tddau_d').$xls->hcell('tddau_p').
            $xls->hcell('tddau_g').$xls->hcell('tddau_s').
            $xls->hcell('hddau_d').$xls->hcell('hddau_p').
            $xls->hcell('hddau_g').$xls->hcell('hddau_s').
            $xls->hcell('tdgiua_d').$xls->hcell('tdgiua_p').
            $xls->hcell('tdgiua_g').$xls->hcell('tdgiua_s').
            $xls->hcell('hdogiua_d').$xls->hcell('hdogiua_p').
            $xls->hcell('hdogiua_g').$xls->hcell('hdogiua_s').
            $xls->hcell('tdcuoi_d').$xls->hcell('tdcuoi_p').
            $xls->hcell('tdcuoi_g').$xls->hcell('tdcuoi_s').
            $xls->hcell('hdocuoi_d').$xls->hcell('hdocuoi_p').
            $xls->hcell('hdocuoi_g').$xls->hcell('hdocuoi_s').$xls->hcell('date_sua')
        );
    foreach ( $tuyens as $result ):
    $cells = '';
        $cells .= $xls->Cell($result['tuyens']['id']);
        $cells .= $xls->Cell($result['tuyens']['tentuyen']);
        $cells .= $xls->Cell($result['tuyens']['tddau']);
        $cells .= $xls->Cell($result['tuyens']['hddau']);
        $cells .= $xls->Cell($result['tuyens']['tdgiua']);
        $cells .= $xls->Cell($result['tuyens']['hdogiua']);
        $cells .= $xls->Cell($result['tuyens']['tdcuoi']);
        $cells .= $xls->Cell($result['tuyens']['hdocuoi']);
        $cells .= $xls->Cell($result['tuyens']['tddau_d']);
        $cells .= $xls->Cell($result['tuyens']['tddau_p']);
        $cells .= $xls->Cell($result['tuyens']['tddau_g']);
        $cells .= $xls->Cell($result['tuyens']['tddau_s']);
        $cells .= $xls->Cell($result['tuyens']['hddau_d']);
        $cells .= $xls->Cell($result['tuyens']['hddau_p']);
        $cells .= $xls->Cell($result['tuyens']['hddau_g']);
        $cells .= $xls->Cell($result['tuyens']['hddau_s']);
        $cells .= $xls->Cell($result['tuyens']['tdgiua_d']);
        $cells .= $xls->Cell($result['tuyens']['tdgiua_p']);
        $cells .= $xls->Cell($result['tuyens']['tdgiua_g']);
        $cells .= $xls->Cell($result['tuyens']['tdgiua_s']);
        $cells .= $xls->Cell($result['tuyens']['hdogiua_d']);
        $cells .= $xls->Cell($result['tuyens']['hdogiua_p']);
        $cells .= $xls->Cell($result['tuyens']['hdogiua_g']);
        $cells .= $xls->Cell($result['tuyens']['hdogiua_s']);
        $cells .= $xls->Cell($result['tuyens']['tdcuoi_d']);
        $cells .= $xls->Cell($result['tuyens']['tdcuoi_p']);
        $cells .= $xls->Cell($result['tuyens']['tdcuoi_g']);
        $cells .= $xls->Cell($result['tuyens']['tdcuoi_s']);
        $cells .= $xls->Cell($result['tuyens']['hdocuoi_d']);
        $cells .= $xls->Cell($result['tuyens']['hdocuoi_p']);
        $cells .= $xls->Cell($result['tuyens']['hdocuoi_g']);
        $cells .= $xls->Cell($result['tuyens']['hdocuoi_s']);
        $cells .= $xls->Cell($result['tuyens']['date_sua']);

        $rows .= $xls->row($cells);   
    endforeach; 
    $worksheets .= $xls->worksheet('tuyens',$rows);
    
     $rows = $xls->row($xls->hcell('id').$xls->hcell('tuyen_id').
                            $xls->hcell('huyen_id').
                            
                            $xls->hcell('date_sua')
                              );
    foreach ( $thuochs as $result ):
    $cells = '';
        $cells .= $xls->Cell($result['thuoc_hs']['id']);
        $cells .= $xls->Cell($result['thuoc_hs']['tuyen_id']);
        $cells .= $xls->Cell($result['thuoc_hs']['huyen_id']);
        $cells .= $xls->Cell($result['thuoc_hs']['date_sua']);

        $rows .= $xls->row($cells);   
    endforeach; 
    $worksheets .= $xls->worksheet('thuoc_hs',$rows);
    
    
    $rows = $xls->row($xls->hcell('id').$xls->hcell('ap_id').
                            $xls->hcell('hoten').$xls->hcell('dchi').
                            $xls->hcell('sdt').$xls->hcell('xa_id').
                            $xls->hcell('date_sua'));
    foreach ( $chuhos as $result ):   
    $cells = '';
       $cells .= $xls->Cell($result['chuhos']['id']);
       $cells .= $xls->Cell($result['chuhos']['ap_id']);
       $cells .= $xls->Cell($result['chuhos']['hoten']);
       $cells .= $xls->Cell($result['chuhos']['dchi']);
       $cells .= $xls->Cell($result['chuhos']['sdt']);
       $cells .= $xls->Cell($result['chuhos']['xa_id']);
       $cells .= $xls->Cell($result['chuhos']['date_sua']);

        $rows .= $xls->row($cells);
    endforeach; 
    $worksheets .= $xls->worksheet('chuhos',$rows);
    
     
     $rows = $xls->row($xls->hcell('id').$xls->hcell('tendv'));
     foreach ( $dvtinhs as $result ):   
    $cells = '';
       $cells .= $xls->Cell($result['dvtinhs']['id']);
       $cells .= $xls->Cell($result['dvtinhs']['tendv']);
      
    endforeach; 
    $worksheets .= $xls->worksheet('dvtinhs',$rows);
    
    
    
    $rows = $xls->row($xls->hcell('id').$xls->hcell('canbo_id').
                            $xls->hcell('tuyen_id').$xls->hcell('chuho_id').$xls->hcell('ngay_lap')
                            .$xls->hcell('ngay_bd_nuoi').$xls->hcell('tg_nuoi').$xls->hcell('thgian_id')
                            .$xls->hcell('date_sua'));
    foreach ( $sotays as $result ):
        $cells = '';
        $cells .= $xls->Cell($result['sotays']['id']);
        $cells .= $xls->Cell($result['sotays']['canbo_id']);
        $cells .= $xls->Cell($result['sotays']['tuyen_id']);
        $cells .= $xls->Cell($result['sotays']['chuho_id']);
        $cells .= $xls->Cell($result['sotays']['ngay_lap']);
        $cells .= $xls->Cell($result['sotays']['ngay_bd_nuoi']);
        $cells .= $xls->Cell($result['sotays']['tg_nuoi']);
        $cells .= $xls->Cell($result['sotays']['thgian_id']);
        $cells .= $xls->Cell($result['sotays']['date_sua']);
        $rows .= $xls->row($cells);
    endforeach; 
    $worksheets .= $xls->worksheet('sotays', $rows);
    
    
    $rows = $xls->row($xls->hcell('id').$xls->hcell('sotay_id').
                            $xls->hcell('ten_phieu').$xls->hcell('ghichu_phieu').
                             $xls->hcell('thutu_phieu').$xls->hcell('maup_id').
                              $xls->hcell('deleted').$xls->hcell('tinh_id').
                              $xls->hcell('date_sua')
                            );
    foreach ( $phieudts as $result ): 
        $cells = '';  
        $cells .= $xls->Cell($result['phieu_dts']['id']) ;
        $cells .= $xls->Cell($result['phieu_dts']['sotay_id']) ;
        $cells .= $xls->Cell($result['phieu_dts']['ten_phieu']); 
        $cells .= $xls->Cell($result['phieu_dts']['ghichu_phieu']); 
        $cells .= $xls->Cell($result['phieu_dts']['thutu_phieu']);
        $cells .= $xls->Cell($result['phieu_dts']['maup_id']); 
        $cells .= $xls->Cell($result['phieu_dts']['deleted']);
        $cells .= $xls->Cell($result['phieu_dts']['tinh_id']); 
        $cells .= $xls->Cell($result['phieu_dts']['date_sua']); 
        $rows .= $xls->row($cells);
    endforeach;
    $worksheets .= $xls->worksheet('phieu_dts', $rows);
     
    
    $rows = $xls->row($xls->hcell('id').$xls->hcell('cauh_id').
                            $xls->hcell('phuong_an_id').$xls->hcell('phieu_dt_id'));
    foreach ( $traloitns as $result ):
        $cells = '';  
        $cells .= $xls->Cell($result['traloitns']['id']) ;
        $cells .= $xls->Cell($result['traloitns']['cauh_id']); 
        $cells .= $xls->Cell($result['traloitns']['phuong_an_id']); 
        $cells .= $xls->Cell($result['traloitns']['phieu_dt_id']); 
        $rows .= $xls->row($cells);
    endforeach; 
    $worksheets .= $xls->worksheet('traloitns', $rows);
    
    
    $rows = $xls->row($xls->hcell('id').$xls->hcell('cauh_id').
                            $xls->hcell('phieu_dt_id').$xls->hcell('ndungtraloi'));
    foreach ( $traloiches as $result ):
        $cells = '';   
        $cells .= $xls->Cell( $result['traloi_ches']['id']) ; 
        $cells .= $xls->Cell($result['traloi_ches']['cauh_id']) ; 
        $cells .= $xls->Cell($result['traloi_ches']['phieu_dt_id']); 
        $cells .= $xls->Cell($result['traloi_ches']['ndungtraloi']); 
        $rows .= $xls->row($cells);
    endforeach; 
    $worksheets .= $xls->worksheet('traloi_ches', $rows);
   
    // If you print the output to the screen set the proper headers
    $xls->setHeader($name);
    $data = $xls->workbook($worksheets);
    
    $encData = $cryptastic->encrypt($data, true);
    if($encData === false){
        $session->setFlash('Lỗi mã hóa dữ liệu');
        $this->redirect(array('controller'=>'bks','action'=>'index'));
    }else{
        echo $encData;
    }
?>